<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="栏目名称" prop="columnName">
        <el-input
          v-model="queryParams.columnName"
          placeholder="请输入栏目名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <div class="right-btn">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:column:add']"
        >新增</el-button>
      </div>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:column:add']"
        >新增</el-button>
      </el-col>
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="handleQuery" />
        </el-tooltip>
        <el-tooltip class="item" effect="dark" :content="showSearch ? '隐藏搜索' : '显示搜索'" placement="top">
          <el-button size="mini" circle icon="el-icon-search" @click="showSearch=!showSearch" />
        </el-tooltip>
      </div>
    </el-row> -->

    <el-table
      v-loading="loading"
      :data="columnList"
      row-key="columnId"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      :max-height="height"
    >
      <el-table-column prop="columnName" label="栏目名称" ></el-table-column>
      <el-table-column prop="orderNum" label="排序" align="center"></el-table-column>
      <el-table-column prop="status" label="显示" align="center" >
        <template slot-scope="scope">
          <a class='el-icon-view' :style="{color:(scope.row.status!=0?'#959595':'#0098ff')}" style="font-size: 18px" @click="handleStatuChange(scope.row)"></a>
        </template>
      </el-table-column>
      <!--<el-table-column label="创建时间" align="center" prop="createTime" width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:column:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['system:column:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:column:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改栏目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="栏目名称" prop="columnName">
              <el-input v-model="form.columnName" placeholder="请输入栏目名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="form.parentId !== 0">
            <el-form-item label="上级栏目" prop="parentId">
              <treeselect v-model="form.parentId" :options="columnOptions" :normalizer="normalizer" placeholder="选择上级栏目" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="排序" prop="orderNum">
              <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="备注" maxlength="20" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listColumn, getColumn, delColumn, addColumn, updateColumn, listColumnExcludeChild, changeStatus } from "@/api/system/column";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Column",
  components: { Treeselect },
  data() {
    return {
      height: (document.documentElement.clientHeight || document.body.clientHeight)-190,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      columnList: [],
      // 栏目树选项
      columnOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        columnName: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        columnName: [
          { required: true, message: "栏目名称不能为空", trigger: "blur" }
        ],
        orderNum: [
          { required: true, message: "菜单顺序不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询栏目列表 */
    getList() {
      this.loading = true;
      listColumn(this.queryParams).then(response => {
        this.columnList = this.handleTree(response.data, "columnId");
        this.loading = false;
      });
    },
    /** 转换栏目数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.columnId,
        label: node.columnName,
        children: node.children
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        columnId: undefined,
        parentId: 0,
        columnName: undefined,
        orderNum: undefined,
        remark: undefined,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      if (row != undefined) {
        this.form.parentId = row.columnId;
      }
      this.open = true;
      this.title = "添加栏目";
      listColumn().then(response => {
	        this.columnOptions = this.handleTree(response.data, "columnId");
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getColumn(row.columnId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改栏目";
      });
      listColumnExcludeChild(row.columnId).then(response => {
	        this.columnOptions = this.handleTree(response.data, "columnId");
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.columnId != undefined) {
            updateColumn(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addColumn(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除名称为"' + row.columnName + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delColumn(row.columnId);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    //改变显示状态
    handleStatuChange(row) {
      var num = 0;
      row.status==1?num=0:num=1;
      changeStatus(row.columnId,num);
      row.status=num;
    },
  }
};
</script>
